﻿/*
 * 由SharpDevelop创建。
 * 用户： baobao
 * 日期: 2020/3/18
 * 时间: 20:32
 * 
 * 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
 */
using System;
using libZPlay;
using System.Threading;

namespace Listener
{
	/// <summary>
	/// 播放管理类
	/// </summary>
    class PlayManager : PropertyChangedBase
    {
        readonly int MaxVolume = 100;
        readonly int MinVolume = 0;

        ZPlay Player;
        
        Thread	UpdateThread;
        
        private bool quitUpdate = false;

        public PlayManager()
        {
            Player = new ZPlay();
            CurrentPlayFile = string.Empty;
            
        }
        
        ~PlayManager()
        {
        	if(UpdateThread != null)
        	{
        		StopPlay();
        	}
        }
        
        string CurrentPlayFile { get; set; }

        public void PlaySong(string FilePath)
        {
        	Play(FilePath);
        }

        private void Play(string FilePath)
        {
            TStreamStatus PlayerStatus = GetPlayerStatus();

            if (PlayerStatus.fPlay == true || PlayerStatus.fPause == true)
            	StopPlay();

            Player.OpenFile(FilePath, TStreamFormat.sfAutodetect);
            Player.SetMasterVolume(100, 100);
           
            CurrentPlayFile = FilePath;
            TotalSeconds = GetStreamInfo().Length.sec;
            TotalTime = TStreamInfoUtils.TimeToStr(GetStreamInfo().Length);
            
            Player.StartPlayback();
            if(UpdateThread == null)
            {
            	UpdateThread = new Thread(UpdateSongTime);
            	UpdateThread.Start();
            }
        }
        
        public void StopPlay()
        {
        	quitUpdate = true;
        	if(UpdateThread != null)
        		UpdateThread.Join();
        	Player.StopPlayback();
        	quitUpdate = false;
        	UpdateThread = null;
        	CurTime = "00:00";
        	CurProgressPercent = 0;
        }

        public void ResumePlay()
        {
            TStreamStatus PlayerStatus = GetPlayerStatus();
            if (PlayerStatus.fPause == true)
                Player.ResumePlayback();
        }

        private TStreamStatus GetPlayerStatus()
        {
            TStreamStatus t_status = new TStreamStatus();
            Player.GetStatus(ref t_status);
            return t_status;
        }

        private int _CurVolume;
        public int CurVolume
        {
            get { return _CurVolume; }
            set
            {
                if (MinVolume <= value && value <= MaxVolume)
                    _CurVolume = value;
                else if (value < MinVolume)
                    _CurVolume = MinVolume;
                else if (value > MaxVolume)
                    _CurVolume = MaxVolume;
                InvokePropertyChanged("CurVolume");
            }
        }

        private float _CurProgressPercent;
        public float CurProgressPercent
        {
            get { return _CurProgressPercent; }
            set { _CurProgressPercent = value; InvokePropertyChanged("CurProgressPercent"); }
        }

        private uint _TotalSeconds;
        public uint TotalSeconds
        {
            get { return _TotalSeconds; }
            set { _TotalSeconds = value; InvokePropertyChanged("TotalSeconds"); }
        }

        private string _TotalTime;
        public string TotalTime
        {
            get { return _TotalTime; }
            set { _TotalTime = value; InvokePropertyChanged("TotalTime"); }
        }

        private string _CurTime;
        public string CurTime
        {
            get { return _CurTime; }
            set { _CurTime = value; InvokePropertyChanged("CurTime"); }
        }
        
        private void UpdateSongTime()
        {
        	TStreamTime timeinfo = new TStreamTime();
            while (true)
            {
                Player.GetPosition(ref timeinfo);
                CurProgressPercent = (int)(((double)timeinfo.sec / (double)TotalSeconds) * 100);
                CurTime = TStreamInfoUtils.TimeToStr(timeinfo);
                if (timeinfo.sec == TotalSeconds || quitUpdate == true)
                    break;
                Thread.Sleep(1000);
            }
            UpdateThread = null;
        }
        
        private TStreamInfo GetStreamInfo()
        {
        	TStreamInfo info = new TStreamInfo();
        	Player.GetStreamInfo(ref info);
        	return info;
        }
    }
}
